package com.example.stgv;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class AttStudent extends Activity {

	private DBHelper db;
	LopHocsData lop = new LopHocsData();
	HocSinhData hs = new HocSinhData();
	Spinner sp_lophoc, sp_hoten, sp_loaiDD;
	ListView lsvDanhSach;
	TextView txthoten, txtngay;
	String slmalop, mahs;
	String[] loaiDD;
	Button btnThem, btnSua, btnXoa, btnThoat;
	int maDD, maTen;
	String[] hocsinhdd;
	ArrayList<String> hocsinh;

	// ArrayList<String> LoaiDD =

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_att_student);
		loadDataToLop();
		/*
		 * Resources resources = getResources(); loaiDD =
		 * resources.getStringArray(R.array.pref_list_values);
		 */

		lsvDanhSach = (ListView) findViewById(R.id.AttStudent_lstDanhSach);
		hocsinh = new ArrayList<String>();

		sp_loaiDD = (Spinner) findViewById(R.id.spAttLoaiDD);
		maDD = 0;
		sp_loaiDD
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						maDD = arg2;
						Log.d("AttStudent ", "loai phep "
								+ sp_loaiDD.getAdapter().getItem(arg2));
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {

					}
				});

		SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy, HH:mm:ss",
				java.util.Locale.getDefault());
		Date todays = new Date();
		txtngay = (TextView) findViewById(R.id.AttStudent_txtngay);
		txtngay.setText(sdf.format(todays).toString());

		btnThem = (Button) findViewById(R.id.att_btnThem);
		btnThoat = (Button) findViewById(R.id.att_btnthoat);
		btnSua = (Button) findViewById(R.id.att_btnSua);
		btnXoa = (Button) findViewById(R.id.att_btnXoa);

		btnThem.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (checkData()) {
					addDiemDanh();

				} else {
					Toast.makeText(AttStudent.this,
							"Lớp này chưa có học sinh.", Toast.LENGTH_SHORT)
							.show();
				}

			}
		});

		btnThoat.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();

			}
		});
	}

	protected boolean checkData() {
		if (sp_hoten.getSelectedItem().toString().isEmpty()) {
			return false;

		}
		return true;
	}

	protected void addDiemDanh() {
		// TODO Auto-generated method stub
		hocsinhdd = new String[3];
		hocsinhdd[0] = sp_lophoc.getSelectedItem().toString();
		hocsinhdd[1] = sp_hoten.getSelectedItem().toString();
		hocsinhdd[2] = sp_loaiDD.getSelectedItem().toString();

		String chuoi = hocsinhdd[0] + " - " + hocsinhdd[1] + " - "
				+ hocsinhdd[2];
		hocsinh.add(chuoi);

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				R.xml.mylist_item, hocsinh);
		lsvDanhSach.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			Intent openSetting = new Intent(this, SettingsActivity.class);
			startActivity(openSetting);
			finish();
		} else if (id == R.id.act_lich) {
			Intent openCalendar = new Intent(this, MainActivity.class);
			startActivity(openCalendar);
		} else if (id == R.id.act_diem) {
			Intent openInputScore = new Intent(this, InputScore.class);
			startActivity(openInputScore);
		} else if (id == R.id.action_newHS) {
			Intent openStudent = new Intent(this, AddStudent.class);
			startActivity(openStudent);
		} else if (id == R.id.action_newLop) {
			Intent openClass = new Intent(this, AddClass.class);
			startActivity(openClass);
		} else {

		}
		return super.onOptionsItemSelected(item);
	}

	protected void loadDataToLop() {
		sp_lophoc = (Spinner) this.findViewById(R.id.AttStudent_spLop);

		ArrayList<String> arrLopHoc = new ArrayList<String>();
		final ArrayList<String> arrMalop = new ArrayList<String>();
		db = new DBHelper(AttStudent.this);
		Cursor cur = db
				.runAllDateRQ("select distinct malop, tenlop from lop order by tenlop");
		if (cur.moveToFirst()) {
			do {
				arrLopHoc.add(cur.getString(1));
				arrMalop.add(cur.getString(0));
			} while (cur.moveToNext());
		}
		cur.close();
		db.close();
		ArrayAdapter<String> l_arrayAdapter = new ArrayAdapter<String>(
				this.getApplicationContext(), R.xml.myspiner_item, arrLopHoc);
		l_arrayAdapter.setDropDownViewResource(R.xml.myspiner_item);
		sp_lophoc.setAdapter(l_arrayAdapter);
		sp_lophoc.setSelection(0);
		sp_lophoc
				.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

					@Override
					public void onItemSelected(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {

						slmalop = arrMalop.get((int) arg3).toString();
						Log.d("AttStudent ", "Da chon Lop " + slmalop);

						loadDataHocSinh(slmalop);
					}

					@Override
					public void onNothingSelected(AdapterView<?> arg0) {

					}
				});
	}

	protected void loadDataHocSinh(String sllophoc) {
		sp_hoten = (Spinner) this.findViewById(R.id.AttStudent_spHS);

		String[] values = new String[1];
		values[0] = sllophoc;
		ArrayList<String> arrhocsinh = new ArrayList<String>();
		final ArrayList<String> arrmahocsinh = new ArrayList<String>();
		db = new DBHelper(AttStudent.this);
		Cursor cur = db.runAllDateRQ(
				"select mahs,tenhs from hocsinh where malop=?", values);
		if (cur.moveToFirst()) {
			do {
				arrhocsinh.add(cur.getString(1));
				arrmahocsinh.add(cur.getString(0));
			} while (cur.moveToNext());
		}
		cur.close();
		db.close();
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(
				this.getApplicationContext(), R.xml.myspiner_item, arrhocsinh);
		adapter.setDropDownViewResource(R.xml.myspiner_item);
		sp_hoten.setAdapter(adapter);
		sp_hoten.setSelection(0);
		sp_hoten.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				maTen = arg2;
				mahs = arrmahocsinh.get((int) arg2).toString();
				Log.d("AttStudent ", "Da chon Lop " + mahs);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}
}
